package com.tzg.iot.http.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;

/**
 * @author tzg157
 */
public class HttpClientUtil {

    public static InputStream get(String ip,int port,String path,String data,String token) throws IOException {
        Socket socket = null;
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        socket = new Socket(ip, port);
        outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
        outputStreamWriter.write("GET " + path + " HTTP/1.1\r\n");
        outputStreamWriter.write("Host: " + ip + "\r\n");

        if(token != null){
            outputStreamWriter.write("X-Authorization: Bearer " + token + "\r\n");
        }
        outputStreamWriter.write("\r\n");
        outputStreamWriter.flush();
        return socket.getInputStream();
    }

    public static InputStream get(String hostName,String data,String token) throws IOException {
        URL url = new URL(hostName);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type","application/json");
        connection.setRequestProperty("X-Authorization","Bearer " + token);
        connection.connect();

//        try (OutputStream os = connection.getOutputStream()) {
//            os.write(data.getBytes("UTF-8"));
//        }
        return connection.getInputStream();
    }

    public static InputStream post(String ip,int port,String path,String data) throws IOException {
        //建立socket连接
        Socket socket = new Socket(ip, port);

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
        //header要有/r/n换行符
        outputStreamWriter.write("POST " + path + " HTTP/1.1\r\n");
        outputStreamWriter.write("Host: " + ip + "\r\n");
        outputStreamWriter.write("Content-Length: " + data.length() + "\r\n");
        outputStreamWriter.write("Content-Type: application/json:charset=UTF-8\r\n");
        // post参数和header之间有 一个 空行
        outputStreamWriter.write("\r\n");
        outputStreamWriter.write(data);
        outputStreamWriter.write("\r\n");
        outputStreamWriter.flush();

        return socket.getInputStream();
    }

    public static InputStream post(String hostName,String data) throws IOException {
        URL url = new URL(hostName);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type","application/json");
        connection.connect();

        try (OutputStream os = connection.getOutputStream()) {
            os.write(data.getBytes("UTF-8"));
        }
        return connection.getInputStream();
    }
}
